// eslint-disable-next-line import/no-unresolved
// import * as XLSX from 'xlsx';

// 时间戳转换
export function formatDate(cellValue: string | number) {
  if (cellValue === null || cellValue === '') return '';
  let date = new Date(cellValue);
  let year = date.getFullYear();
  let month =
    date.getMonth() + 1 < 10 ? `0${date.getMonth()}` : date.getMonth() + 1;
  let day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
  let hours = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
  let minutes =
    date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
  let seconds =
    date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
  // return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 将时间戳转换为标准时间
export function timeSwitch(time: number) {
  if (String(time).split('').length < 13) time *= 1000;
  time = Number(formatDate(time));
  return time;
}

// 获取当前的时间，年月日
export function getNowDate() {
  let date = new Date();
  let sign2 = ':';
  let year = date.getFullYear(); // 年
  let month: number | string = date.getMonth() + 1; // 月
  let day: number | string = date.getDate(); // 日
  // let hour = date.getHours(); // 时
  // let minutes = date.getMinutes(); // 分
  // let seconds = date.getSeconds() //秒
  let weekArr = [
    '星期一',
    '星期二',
    '星期三',
    '星期四',
    '星期五',
    '星期六',
    '星期天',
  ];
  let week = weekArr[date.getDay()];
  // 给一位数的数据前面加 “0”
  if (month >= 1 && month <= 9) {
    month = `0${month}`;
  }
  if (day >= 0 && day <= 9) {
    day = `0${day}`;
  }
  // if (hour >= 0 && hour <= 9) {
  //   hour = "0" + hour;
  // }
  // if (minutes >= 0 && minutes <= 9) {
  //   minutes = "0" + minutes;
  // }
  // if (seconds >= 0 && seconds <= 9) {
  //   seconds = "0" + seconds;
  // }
  // return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
  return `${year}${month}${day}`;
}

// 将树形结构扁平化
export function flatTree(arrs: any, childs: string, attrArr?: any) {
  let attrList: any = [];
  if (!Array.isArray(arrs) && !arrs.length) return [];
  if (typeof childs !== 'string') return [];
  if (!Array.isArray(attrArr) || (Array.isArray(attrArr) && !attrArr.length)) {
    attrList = Object.keys(arrs[0]);
    attrList.splice(attrList.indexOf(childs), 1);
  } else {
    attrList = attrArr;
  }
  let list: any = [];
  const getObj = (arr: any) => {
    arr.forEach(function (row: any) {
      let obj = {};
      attrList.forEach((item: any) => {
        obj[item] = row[item];
      });
      list.push(obj);
      if (row[childs]) {
        getObj(row[childs]);
      }
    });
    return list;
  };
  return getObj(arrs);
}

// 导出excel
/*
    * @description:
    * @param {Object} json 服务端发过来的数据
    * @param {String} name 导出Excel文件名字

    * @param {String} titleArr 导出Excel表头

    * @param {String} sheetName 导出sheetName名字
    * @return:
    */
// export function exportExcel(json, name, titleArr, sheetName) {
//   /* convert state to workbook */
//   console.log(json);

//   let data = [];
//   let keyArray = [];
//   const getLength = function (obj) {
//     let count = 0;
//     for (let i in obj) {
//       // if (obj.hasOwnProperty(i)) {
//       //   count++;
//       // }
//     }
//     return count;
//   };
//   for (const key1 in json) {
//     // console.log(key1 + 'a');

//     // eslint-disable-next-line no-prototype-builtins
//     if (json.hasOwnProperty(key1)) {
//       const element = json[key1];
//       let rowDataArray = [];
//       for (const key2 in element) {
//         if (element.hasOwnProperty(key2) && key2 !== 'id') {
//           const element2 = element[key2];
//           rowDataArray.push(element2);
//           if (keyArray.length < getLength(element)) {
//             keyArray.push(key2);
//           }
//         }
//       }
//       data.push(rowDataArray);
//     }
//   }
//   // keyArray为英文字段表头
//   // data.splice(0, 0, keyArray,titleArr);
//   data.splice(0, 0, titleArr);
//   console.log('data', data);
//   const ws = XLSX.utils.aoa_to_sheet(data);
//   const wb = XLSX.utils.book_new();
//   // 此处隐藏英文字段表头
//   // let wsrows = [{ hidden: true }];
//   // ws['!rows'] = wsrows; // ws - worksheet
//   XLSX.utils.book_append_sheet(wb, ws, sheetName);
//   /* generate file and send to client */
//   XLSX.writeFile(wb, name + '.xlsx');
// }
